// 反转一个单链表
/**
 * @param {ListNode} head
 * @return {ListNode}
 */


// ====================================迭代===============================
const Node = require('../../单链表/node')
var reverseList = function(head) {
    // 虚拟头节点
    var viHead = new Node
    //空表及单节点表容错
    if(head === null || head.next === null) return head
    //头插法迭代
    var p = head
    var r = head.next
    while(p !== null) {
        p.next = viHead.next
        viHead.next = p
        p = r
        if(r !== null) {
            r = r.next
        }
    }
    return viHead.next
};


// ===================================递归===================================
var reverseList = function (head) {
    if (!head || !head.next) return head;
    let p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
};

